package router

import (
	"fmt"
	"github.com/gin-gonic/gin"
	path2 "path"
	"server/acgnovel-go/src/helper"
	"server/acgnovel-go/src/utils"
)

//实现授权过滤
func AuthFilter() gin.HandlerFunc {
	return func(c *gin.Context) {
		if isExcludePath(c.Request.URL.Path) {
			//执行下一步
			c.Next()
		} else {
			if isLogin(utils.GetToken(c)) {
				//执行下一步
				c.Next()
			} else {
				utils.ResultFail(c, "您还未登陆")
				//不再执行下面的处理
				c.Abort()
			}

		}
	}
}

//排除的路径
func isExcludePath(path string) bool {
	switch path {
	case "/favicon.ico":
		return true
	case "/login":
		return true
	}
	ok, err := path2.Match("/static/*", path)
	if ok {
		return true
	}
	if err != nil {
		fmt.Println(err)
	}
	return false
}

func isLogin(token string) bool {
	exists := helper.RedisHelperExists(token)
	if exists == 0 {
		return false
	}
	return true
}
